<template>
  <div class="distribution-recruit-set gray-box top-redius border-bottom-gray">
    <div class="title">
      {{ $t("user.pointsQuestionSetting") }}
    </div>
    <el-form
      ref="dataFormRef"
      label-width="100px"
      class="set-form"
      label-position="left"
      :rules="dataRule"
      :model="dataForm"
      @submit.prevent
    >
      <el-form-item
        :label="$t('user.pointsQusetion')"
        prop="paramValue"
      >
        <tiny-mce
          ref="tinyMceRef"
          :key="tinymceFlag"
          v-model="dataForm.paramValue"
        />
      </el-form-item>
    </el-form>
    <div
      class="default-btn"
      @click="onSubmit()"
    >
      {{
        $t("sysManagement.save")
      }}
    </div>
  </div>
</template>

<script setup>
import { validHtmlLength } from '@/utils/index.js'
import { ElMessage } from 'element-plus'
const dataForm = ref({
  paramValue: ''
})
const dataRule = {
  paramValue: [
    { required: true, message: $t('product.content'), trigger: 'brue' }
  ]
}
onMounted(() => {
  onGetData()
})

// 获取数据
const tinyMceRef = ref(null)
const dataFormRef = ref(null)
const onGetData = () => {
  nextTick(() => {
    http({
      url: http.adornUrl('/user/scoreExplainConfig/info/' + 'SCORE_QUESTION'),
      method: 'get',
      params: http.adornParams()
    }).then(({ data }) => {
      if (data !== null) {
        dataForm.value = data
      }
    })
    dataFormRef.value.resetFields()
  })
}
// 提交表单
const onSubmit = () => {
  dataFormRef.value.validate((valid) => {
    if (valid) {
      if (validHtmlLength(dataForm.value.paramValue)) {
        return
      }
      if (dataForm.value.paramValue && dataForm.value.paramValue.length > 20000) {
        return ElMessage({
          message: $t('product.tinyMceTip'),
          type: 'error',
          duration: 1500
        })
      }
      http({
        url: http.adornUrl('/user/scoreExplainConfig'),
        method: dataForm.value.id ? 'put' : 'post',
        data: http.adornData({
          paramKey: 'SCORE_QUESTION',
          paramValue: dataForm.value.paramValue,
          remark: $t('user.pointsQuestionSetting')
        })
      }).then(() => {
        ElMessage({
          message: $t('publics.operation'),
          type: 'success',
          duration: 1500
        })
      })
    }
  })
}
// keep-alive缓存的缘故 需调用钩子函数 设置key值不然编辑器无法使用
const tinymceFlag = ref(new Date() + 1)
onActivated(() => {
  tinymceFlag.value = new Date() + 1
})

</script>
